package org.asiainfo.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.asiainfo.common.core.domain.R;
import org.asiainfo.common.idempotent.annotation.RepeatSubmit;
import org.asiainfo.common.log.annotation.Log;
import org.asiainfo.common.log.enums.BusinessType;
import org.asiainfo.common.mybatis.stream.core.page.PageQuery;
import org.asiainfo.common.mybatis.stream.core.page.TableDataInfo;
import org.asiainfo.common.web.core.BaseController;
import org.asiainfo.system.domain.bo.SysDictTypeBo;
import org.asiainfo.system.domain.vo.SysDictTypeVo;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.validation.annotation.Validated;
import org.asiainfo.system.service.SysDictTypeService;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 字典类型表
 *
 * @author dotor-ww
 */
@RestController
@Validated
@RequestMapping("/dictType")
@RequiredArgsConstructor
public class SysDictTypeController extends BaseController {

    private final SysDictTypeService sysDictTypeService;

    /**
     * 查询所有字典类型表
     *
     * @param dictTypeBo 字典类型
     * @param pageQuery  分页信息
     * @return 字典类型
     */
    @SaCheckPermission("system:dict:list")
    @GetMapping("/list")
    public TableDataInfo<SysDictTypeVo> list(SysDictTypeBo dictTypeBo, PageQuery pageQuery) {
        return sysDictTypeService.selectPageDictTypeList(dictTypeBo, pageQuery);
    }

    /**
     * 根据字典类型表主键获取详细信息。
     *
     * @param dictId sysDictType主键
     * @return 字典类型表详情
     */
    @SaCheckPermission("system:dict:query")
    @GetMapping("/{dictId}")
    public R<SysDictTypeVo> getInfo(@PathVariable Long dictId) {
        return R.ok(sysDictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 添加 字典类型表
     *
     * @param dict 字典类型表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @SaCheckPermission("system:dict:add")
    @Log(title = "字典类型表", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping
    public R<Void> add(@Validated @RequestBody SysDictTypeBo dict) {
        if (sysDictTypeService.checkDictTypeUnique(dict)) {
            return R.fail("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        sysDictTypeService.insertDictType(dict);
        return R.ok();
    }

    /**
     * 根据主键更新字典类型表
     *
     * @param dict 字典类型表
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @SaCheckPermission("system:dict:edit")
    @Log(title = "字典类型表", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping
    public R<Void> update(@Validated @RequestBody SysDictTypeBo dict) {
        if (sysDictTypeService.checkDictTypeUnique(dict)) {
            return R.fail("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        sysDictTypeService.updateDictType(dict);
        return R.ok();
    }

    /**
     * 根据主键删除字典类型表
     *
     * @param dictIds 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @SaCheckPermission("system:dict:remove")
    @Log(title = "字典类型表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}")
    public R<Void> remove(@NotNull(message = "主键ID不能为空") @PathVariable Long[] dictIds) {
        sysDictTypeService.deleteDictTypeByIds(dictIds);
        return R.ok();
    }
    /**
     * 刷新字典缓存
     */
    @SaCheckPermission("system:dict:remove")
    @Log(title = "字典类型表", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public R<Void> refreshCache() {
        sysDictTypeService.resetDictCache();
        return R.ok();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionSelect")
    public R<List<SysDictTypeVo>> optionSelect() {
        List<SysDictTypeVo> dictTypes = sysDictTypeService.selectDictTypeAll();
        return R.ok(dictTypes);
    }
}
